package com.zimi.service;

import com.zimi.mapper.WfActivityMapper;
import com.zimi.mapper.WfActivityVOMapper;
import com.zimi.mapper.WfWorkflowMapper;
import com.zimi.model.WfActivity;
import com.zimi.model.WfActivityVO;
import com.zimi.model.WfWorkflow;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WfActivityService {

    @Autowired
    WfWorkflowMapper wfWorkflowMapper;
    @Autowired
    WfActivityMapper wfActivityMapper;
    @Autowired
    WfActivityVOMapper wfActivityVOMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfActivityMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfActivity record) {
        return wfActivityMapper.insert(record);
    }


    public int insertSelective(WfActivity record) {
        return wfActivityMapper.insertSelective(record);
    }


    public WfActivity selectByPrimaryKey(Integer id) {
        return wfActivityMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfActivity record) {
        return wfActivityMapper.updateByPrimaryKeySelective(record);
    }


    public int updateByPrimaryKey(WfActivity record) {
        return wfActivityMapper.updateByPrimaryKey(record);
    }


    public List<WfActivity> selectAll(WfActivity record) {
        return wfActivityMapper.selectAll(record);
    }

    public List<WfActivityVO> selectAll(WfActivityVO record) {
        return wfActivityVOMapper.selectAllVO(record);
    }

    public List<WfActivity> selectWfNull(WfActivity record) {
        return wfActivityVOMapper.selectWfNull(record);
    }


    public List<WfActivity> listAllByWfActivityNoStartEnd(WfActivity record) {
        List<WfActivity> wfaList = new ArrayList<>();
        WfWorkflow wf = new WfWorkflow();
        if (record != null && record.getWfId() != null) {
            wf = wfWorkflowMapper.selectByPrimaryKey(record.getWfId());
        }
        if (wf != null) {
            wfaList = wfActivityMapper.listAllByWfActivityNoStartEnd(record);
        }
        return wfaList;
    }

    /*
     * @author: wj
     * @description: 新建活动
     * @date: 2019/3/29 11:04
     * @param
     * @return
     */
    public WfActivity saveNewActivity(WfActivity wfActivity) {
        String activityCode = "";
        String mm = wfActivity.getActivityType();
        if ("end".equals(mm)) {
            activityCode = "end";
        } else {
            activityCode = calculateActivityCode(wfActivity.getWfId(), 0);
        }
        wfActivity.setActivityCode(activityCode);
        wfActivityMapper.insertSelective(wfActivity);
        int wfaId = wfActivityMapper.lastInsertId();
        wfActivity.setWfaId(wfaId);
        return wfActivity;
    }

    /*
     * @author: wj
     * @description: 计算活动代码（仅支持数字型代码）
     * @date: 2019/3/29 11:04
     * @param
     * @return
     */
    private String calculateActivityCode(Integer wfCode, Integer activityId) {
        String code = "001";
        WfActivityVO wfActivityVO = new WfActivityVO();
        if (wfCode != null && wfCode.intValue() != 0) {
            wfActivityVO.setWfId(wfCode);
        } else {
            wfActivityVO.setWfaId(activityId);
        }
        List<WfActivityVO> list = wfActivityVOMapper.selectAllVO(wfActivityVO);
        if (list.size() == 0)
            return code;
        String maxCode = list.get(0).getActivityCode();
        int codeInt = 1;
        for (WfActivityVO act : list) {
            String tmpCode = act.getActivityCode();
            int tempCodeInt = 0;
            try {
                if(!"state".equals(tmpCode) && ! "end".equals(tmpCode)){
                    tempCodeInt = Integer.parseInt(tmpCode);
                    if (tempCodeInt >= codeInt)
                        codeInt = tempCodeInt + 1;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        String codeString = codeInt + "";
        while (codeString.length() < 3) {
            codeString = "0" + codeString;
        }
        return codeString;
    }

    /*
     * @author: wj
     * @description: 创建开始和结束活动，创建流程时调用
     * @date: 2019/3/29 19:37
     * @param
     * @return
     */
    public Map save_createStartAndEndActivity(int wfId) {
        WfActivity startActivity = new WfActivity();
        WfActivity endActivity = new WfActivity();
        WfActivity firstActivity = new WfActivity();


        startActivity.setWfId(wfId);

        BeanUtils.copyProperties(startActivity, endActivity);
        BeanUtils.copyProperties(startActivity, firstActivity);

        startActivity.setPositionX("100");
        startActivity.setPositionY("100");
        startActivity.setActivityName("开始");
        startActivity.setActivityType("start");
        startActivity.setActivityCode("start");

        endActivity.setPositionX("900");
        endActivity.setPositionY("600");
        endActivity.setActivityName("结束");
        endActivity.setActivityType("end");
        endActivity.setActivityCode("end");

        firstActivity.setPositionX("100");
        firstActivity.setPositionY("200");
        firstActivity.setActivityName("提交申请");
        firstActivity.setActivityType("simple");
        firstActivity.setActivityCode("001");
        firstActivity.setParticipateType("2");
        firstActivity.setOperationType("4");
        firstActivity.setActivityButtons("10,20");

        wfActivityMapper.insertSelective(startActivity);
        int startWfaId = wfActivityMapper.lastInsertId();
        startActivity.setWfaId(startWfaId);
        wfActivityMapper.insertSelective(endActivity);
        wfActivityMapper.insertSelective(firstActivity);
        int firstWfaid = wfActivityMapper.lastInsertId();
        firstActivity.setWfaId(firstWfaid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startActivity", startActivity);
        map.put("firstActivity", firstActivity);
        return map;
    }
}
